package com.lx.service.impl;

import com.lx.dao.UserDao;
import com.lx.dao.impl.UserDaoImpl;
import com.lx.entity.User;
import com.lx.entity.vo.UserVo;
import com.lx.service.UserService;
import com.lx.uitl.ConnUtil;
import com.lx.uitl.MD5Util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author c先生
 * @date 2022年05月05日 下午 4:05:35
 * @describe
 */

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();

    @Override
    public Integer registerUser(User user) {
        Integer result = null;
        try {
            result = userDao.registerUser(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            ConnUtil.closeConnection();
        }
        return result;
    }

    @Override
    public String loginUser(String name) {
        String pwd = null;
        try {
            pwd = userDao.loginUser(name);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            ConnUtil.closeConnection();
        }
        return pwd;
    }

    @Override
    public Integer getUserIdByPhone(String userPhone) {
        Integer id = null;
        try {
            id = userDao.getByPhone(userPhone);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnUtil.closeConnection();
        }
        return id;
    }

    @Override
    public List<UserVo> getAll() {
        List<UserVo> list = null;
        try {
            list = userDao.getAll();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnUtil.closeConnection();
        }
        return list;
    }

    @Override
    public User getUserInfoByName(String userName) {
        try {
            List<User> list = userDao.getUserInfoByName(userName);
            if (list.size() > 0) {
                return list.get(0);
            }
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("SQL执行异常");
            return null;
        } finally {
            ConnUtil.closeConnection();
        }
    }

    @Override
    public boolean isExistByPhoneNum(String phoneNum) {
        try {
            int count = userDao.getUserByPhoneNum(phoneNum);
            if (count > 0) {
                return true;
            }
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            ConnUtil.closeConnection();
        }
    }

    @Override
    public User login(String phoneNum, String pwd) {
        try {
            User user = userDao.getUserByPhone(phoneNum);
            if (user != null) {
                if (phoneNum.equals(user.getUserPhone()) && MD5Util.md5(pwd).equals(user.getUserPwd())) {
                    return user;
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            ConnUtil.closeConnection();
        }
    }

    @Override
    public boolean isExistByIdCard(String idCard) {
        try {
            int count = userDao.userCountByIdCard(idCard);
            if (count > 0) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            ConnUtil.closeConnection();
        }
    }

    @Override
    public boolean openAccount(User user) {
        try {
            int affectRows = userDao.addUser(user);
            if (affectRows > 0) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            ConnUtil.closeConnection();
        }
    }
}
